# 指定CMake的最低版本要求
cmake_minimum_required(VERSION 3.10)

# 项目名称
project(RTSPServer VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找live555库
# 如果系统中已安装live555，可以使用find_package
# 否则，我们将直接链接到源代码

# 检查是否存在live555库目录
set(LIVE555_DIR "${CMAKE_CURRENT_SOURCE_DIR}/live555")

if(NOT EXISTS "${LIVE555_DIR}")
    message(STATUS "live555库未找到，将自动下载并构建")
    
    # 下载live555源代码
    include(ExternalProject)
    
    # 根据不同平台选择合适的live555配置
    if(WIN32)
        set(LIVE555_MAKEFILE mingw)
    elseif(APPLE)
        set(LIVE555_MAKEFILE macosx)
    else()
        set(LIVE555_MAKEFILE linux)
    endif()
    
    ExternalProject_Add(
        live555
        URL http://www.live555.com/liveMedia/public/live555-latest.tar.gz
        PREFIX ${CMAKE_CURRENT_BINARY_DIR}/live555
        CONFIGURE_COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555 && ./genMakefiles ${LIVE555_MAKEFILE}
        BUILD_COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555 && make
        INSTALL_COMMAND ""
        BUILD_IN_SOURCE 1
        # 添加编译选项解决live555库中的常见警告问题
    CMAKE_ARGS -DCMAKE_CXX_FLAGS="-fpermissive -Wno-int-to-pointer-cast -Wno-misleading-indentation -Wno-array-bounds -DNO_STD_LIB=1" -DCMAKE_C_FLAGS="-fpermissive -Wno-int-to-pointer-cast -Wno-misleading-indentation -Wno-array-bounds -DNO_STD_LIB=1"
    )
    
    # 设置live555库和头文件路径
    set(LIVE555_INCLUDE_DIRS 
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/liveMedia/include
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/groupsock/include
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/UsageEnvironment/include
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/BasicUsageEnvironment/include
    )
    
    set(LIVE555_LIBRARIES 
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/liveMedia/libliveMedia.a
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/groupsock/libgroupsock.a
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/UsageEnvironment/libUsageEnvironment.a
        ${CMAKE_CURRENT_BINARY_DIR}/live555/src/live555/BasicUsageEnvironment/libBasicUsageEnvironment.a
    )
else()
    message(STATUS "使用本地live555库")
    
    # 设置本地live555库和头文件路径
    set(LIVE555_INCLUDE_DIRS 
        ${LIVE555_DIR}/liveMedia/include
        ${LIVE555_DIR}/groupsock/include
        ${LIVE555_DIR}/UsageEnvironment/include
        ${LIVE555_DIR}/BasicUsageEnvironment/include
    )
    
    set(LIVE555_LIBRARIES 
        ${LIVE555_DIR}/liveMedia/libliveMedia.a
        ${LIVE555_DIR}/groupsock/libgroupsock.a
        ${LIVE555_DIR}/UsageEnvironment/libUsageEnvironment.a
        ${LIVE555_DIR}/BasicUsageEnvironment/libBasicUsageEnvironment.a
    )
endif()

# 添加源文件
set(SOURCES
    main.cpp
    rtsp_server.cpp
)

# 添加头文件
set(HEADERS
    rtsp_server.h
)

# 创建可执行文件
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

# 如果是下载的live555，需要等待其构建完成
if(NOT EXISTS "${LIVE555_DIR}")
    add_dependencies(${PROJECT_NAME} live555)
endif()

# 包含头文件目录
target_include_directories(${PROJECT_NAME} PRIVATE ${LIVE555_INCLUDE_DIRS})

# 链接live555库
target_link_libraries(${PROJECT_NAME} PRIVATE ${LIVE555_LIBRARIES})

# 在Windows系统上链接ws2_32库
if(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
endif()

# 设置输出路径
set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

# 安装配置（可选）
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)